var type = ['1', '2', '6'];
var param = null;


$(init);

function init() {
    $("body").append(LOADER);

    promise('GET', URL_BUY_INFO, null, function(e) {
        if($.isEmptyObject(e)) {
            goto('/mobile.html');
        }
    });

    notifyToast(MSG_LOGIN_SUCCESS);
    setTimeout(doNewTask, 5000);

    $("#i-mask").remove();


    $('body').on('click', 'ons-button#logout-btn', doLogOut)
        .on('click', 'ons-fab.to-top-btn', backToTop)
        .on('click', 'ons-tab.servicefee', function () {
            initList(2, '/mobile-tmpl/servicefee-render.tmpl');
        })
        .on('click', 'ons-tab.balance-record', function () {
            initList(1, '/mobile-tmpl/balance-render.tmpl');
        })
        .on('click', 'ons-tab.spread', function () {
            initList(6, '/mobile-tmpl/spread-render.tmpl');
        })
        .on('click', 'ons-fab.example', function () {
            navPushPage('/mobile-views/_me/bind-tb-example.html');
        })
        .on('click', 'ons-tab#me', function () {initUserInfo();updateMoney();initVIP();})
        .on('click', 'ons-tab#home', function () { initUserInfo(); });

    document.addEventListener('init', function (event) {
        var page = event.target;
        // console.log(page);
        if (page.matches('#me')) {
            updateMoney();
            initVIP();
        } else if (page.matches('#order-detail')) {
            initList(1, '/mobile-tmpl/balance-render.tmpl');
        }
    });

}

function updateMoney() {
    promise('GET', '/buyer/buyer_balance', null, function(e) {
        $("#user-name").text(cookie('name'));
        $("#all").text(fix(e.balance + e.servicefee));
        $("#servicefee").text(fix(e.servicefee));
        $("#balance").text(fix(e.balance));
        // $("#spread").text( fix(e.spread) );
    }, null);
}

function initVIP () {
    if( cookie('memberValid') ) {
        $('#vip-list-it').show();
        $('#vip-center').text(`会员至 [ ${cookie('memberEndDate')} ]`);
    } else {
        $('#vip-list-it').hide();
    }
}

function pullHoonk() {

    pullHook.addEventListener('changestate', function (event) {
        console.log('Changed to state: ' + event.state);
    });
}

function backToTop() {
    $('div.page__content').animate({
        scrollTop: 0
    }, 'slow');
    return false;
}

function doLogOut() {
    // CLEAR DATA
    if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
        clearData();
    } else if (/(Android)/i.test(navigator.userAgent)) {
        clearDataAndroid();
    }

    Cookies.remove('cko');
    promise('GET', '/exist', null, null);
    goto('/mobile.html');
}

function initList(type, renderTmpl) {
    param = {
        type: type,
        pageIndex: 0,
        pageSize: 9999,

    };
    promiseTmpl('GET', renderTmpl, [URL_BUY_TRADE_RECORD, encodeQuery(param)].join('?'), null, cbList);
}

function cbList(r, e) {
    var ret = e;
    // console.log(ret.data.length);
    if(!ret.data.length) return;
    if (param.type === 2) {
        $('ons-page#servicefee-record div.page__content').html('');
        $('ons-page#servicefee-record div.page__content').append($.templates(r).render(ret, rdHelper));
    } else if (param.type === 6) {
        $('ons-page#spread-record div.page__content').html('');
        $('ons-page#spread-record div.page__content').append($.templates(r).render(ret, rdHelper));
    } else if (param.type === 1) {
        $('ons-page#balance-record div.page__content').html('');
        $('ons-page#balance-record div.page__content').append($.templates(r).render(ret, rdHelper));
    }

}


function doNewTask() {
    if (parseInt(cookie('approveState')) !== 1) {
        $('ons-alert-dialog').remove();
        notifyInfo('新手请先至新手任务完成相关信息绑定，完成所有绑定后，平台奖励2金！');
    }
}